package com.xyz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyz.annotation.AdminRole;
import com.xyz.annotation.LogOperation;
import com.xyz.entity.request.*;
import com.xyz.entity.response.ResultResponsePage;
import com.xyz.entity.vo.ListUserVO;
import com.xyz.entity.response.ResultResponse;
import com.xyz.service.SystemService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 夏永泽
 * @date 2022/4/1
 */
@RestController
public class UserController {
    private final SystemService service;

    public UserController(SystemService service) {
        this.service = service;
    }

    /**
     * 获取用户列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/user")
    public ResultResponsePage<ListUserVO> listUsers(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        Page<ListUserVO> listUserVOS = this.service.listUsers(page, limit);
        return ResultResponsePage.success(listUserVOS);
    }

    /**
     * 获取单个用户
     * @param idNumber
     * @return
     */
    @GetMapping("/user/{idNumber}")
    public ResultResponse<ListUserVO> getUser(@PathVariable String idNumber) {
        ListUserVO user = this.service.getUser(idNumber);
        return ResultResponse.success(user);
    }

    /**
     * 更改用户信息
     * @param request
     * @return
     */
    @PutMapping("/user")
    public ResultResponse<Object> updateUser(@Valid @RequestBody UpdateUserRequest request) {
        this.service.updateUser(request);
        return ResultResponse.success();
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/user/{userId}")
    public ResultResponse<Object> deleteUser(@PathVariable String userId) {
        this.service.deleteUser(Long.parseLong(userId));
        return ResultResponse.success();
    }

    /**
     * 添加用户
     * @param request
     * @return
     */
    @PostMapping("/user")
    public ResultResponse<Object> addUser(@Valid @RequestBody AddUserRequest request) {
        this.service.addUser(request);
        return ResultResponse.success();
    }

    /**
     * 用户注册
     * @param request
     * @return
     */
    @PostMapping("/user/register")
    public ResultResponse<Object> registerUser(@Valid @RequestBody RegisterUserRequest request) {
        AddUserRequest addUserRequest = new AddUserRequest();
        BeanUtils.copyProperties(request, addUserRequest);
        addUserRequest.setRole(2);
        this.service.addUser(addUserRequest);
        return ResultResponse.success();
    }

    /**
     * 修改密码
     * @param request
     * @return
     */
    @PutMapping("/user/password")
    public ResultResponse<Object> changePassword(@RequestBody ChangePasswordRequest request) {
        this.service.changePassword(request);
        return ResultResponse.success();
    }
}
